<?php
include_once '../../__global.php';

class aj_addAttitude extends ml_controller {
    
    private $att;
    private $oid;    //宝贝主人UID
    private $uid;   //喜欢的中间人
    private $rid;    
    private $aid;    //当前动作人UID
    
    function initParam() {
        $this->oid = $this->input('oid', 'P', 0);
        $this->uid = $this->input('uid', 'P', 0);
        $this->rid = $this->input('rid', 'P', 0);
        $this->att = $this->input('att', 'P', ML_ATTITUDE_LOVE);
        $this->aid = $this->__visitor['uid'];    
        
    }
    
    function checkParam() {
     
        //$this->check_referer();
        if ($this->check_permission(ML_PERMISSION_LOGOUT_ONLY)) {
            $this->api_output(ML_RCODE_NOLOGIN);
        }
        
        if ($this->check_permission(ML_PERMISSION_UNVERIFY_ONLY)) {
            $this->api_output(ML_RCODE_NOACTIVE);
        }
        
        if (!ml_tool_resid::is_resid($this->rid)) {
            $this->api_output(ML_RCODE_PARAM,'','wrong rid');
        }
        
        if (!ml_tool_isuid::is_mluid($this->oid)) {
            $this->api_output(ML_RCODE_PARAM,'','wrong oid');
        }
        
        if (!array_key_exists($this->att, array(0,1,2,3))) {
            $this->api_output(ML_RCODE_PARAM, '', 'wrong attitude');
        }
    }
    
    function main() {
        
        if(empty($this->uid)){
            $this->uid = $this->oid;
        }
        
        $from = 1;
        
        $attBiz = new ml_biz_attitude();
        $rs = $attBiz->addAttitude($this->aid, $this->rid, $this->uid, $this->att);
        if (!$rs) {
            $this->api_output(ML_RCODE_FAIL,'', $attBiz->get_errinfo());
        }
        
        $oRedis = new ml_model_rdsCount();
        $oRedis->incrAttitudeCount($this->rid);
        $oRedis->incrUserAttitudeCount($this->uid);
        
        if($this->uid != $this->oid){
            $oRedis->incrUserAttitudeCount($this->oid);    
        }
        
        //发feed
        $ig = new ml_model_dbIdGenerator();
        if (!$rs = $ig->get_userfeed_rid($this->aid, $frid)){
            $this->api_output(ML_RCODE_FAIL,'','feed fail');
        }
        
        $data = array();
        $data['rid'] = $frid;
        $data['uid'] = $this->aid;
        $data['type'] = ML_FEED_TYPE_ATTITUDE;
        $data['content'] = '';
        $data['pic_id'] = '';
        $data['from'] = 1;
        $data['content_rid'] = $this->rid;
        $data['status'] = 1;
        $data['ctime'] = date('Y-m-d H:i:s', time());
        $data['cip'] = Tool_ip::get_real_ip();
        
        $data['feed_info'] = array(
                                     'attitude' => $this->att,
                                     'rid' => $this->rid,
                             );          
        
        if($this->uid != $this->oid){
            $data['feed_info']['middle_uid']=$this->uid;
        }
        
        $feed = new ml_model_dbUserfeed();
        $feed->insert_content($data);
        $oRedis->incrUserfeedcnt($this->aid);
        
        ml_tool_sendMq::userAction2GoodsRank($this->rid, 'like', $this->att);
        
        $data = array('type'=>'atti','content_rid'=>$this->rid,'feed_rid' => $frid, 'uid' => $this->aid);
        ml_tool_sendMq::add_feed($data); 
        
        $oRds = new ml_model_rdsNotice();
        if($this->aid != $this->oid)
        {
            $oRds->add_notice($this->oid, ml_model_rdsNotice::NTCTYPE_ADDATTITUDE);    
        }
        if($this->aid != $this->uid){
            $oRds->add_notice($this->uid, ml_model_rdsNotice::NTCTYPE_ADDATTITUDE);
        }
        
        ml_tool_actionlog::add_attitude($this->aid , $this->rid , $this->att , $attBiz->get_content_tags());
        $this->api_output(ML_RCODE_SUCC);
    }
}

new aj_addAttitude();